/**
 * @Author: lll
 * @Email:li.lele@vpclub.cn
 * @Date: create in 2020/7/13
 */
package cn.vpclub.admin.web.home;

import cn.vpclub.admin.entity.CollectionsEntity;
import cn.vpclub.admin.entity.StarsEntity;
import cn.vpclub.admin.mapper.CollectionsMapper;
import cn.vpclub.admin.param.CollectionsRequestParam;
import cn.vpclub.admin.param.CollectionsResponseParam;
import cn.vpclub.admin.util.BaseResponse;
import cn.vpclub.admin.util.ResponseCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "collections")
public class CollectionsController {
    @Autowired
    CollectionsMapper collectionsMapper;

    /**
     * 收藏
     *
     * @param collectionsRequestParam
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "addCollections", method = RequestMethod.POST)
    public BaseResponse addCollections(@RequestBody @Validated CollectionsRequestParam collectionsRequestParam, HttpServletRequest httpServletRequest) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", httpServletRequest.getAttribute("userId"));
        queryWrapper.eq("ArticleId", collectionsRequestParam.getArticleId());
        List<StarsEntity> list = collectionsMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            return BaseResponse.info(ResponseCodeEnum.HasStared, null);
        }
        CollectionsEntity collectionsEntity = new CollectionsEntity();
        collectionsEntity.setArticleId(collectionsRequestParam.getArticleId());
        collectionsEntity.setUserId((String) httpServletRequest.getAttribute("userId"));
        collectionsMapper.insert(collectionsEntity);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, null);
    }

    /**
     * 是否收藏
     *
     * @param collectionsRequestParam
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "selectCollections", method = RequestMethod.POST)
    public BaseResponse selectCollections(@RequestBody @Validated CollectionsRequestParam collectionsRequestParam, HttpServletRequest httpServletRequest) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", httpServletRequest.getAttribute("userId"));
        queryWrapper.eq("ArticleId", collectionsRequestParam.getArticleId());
        List<CollectionsEntity> list = collectionsMapper.selectList(queryWrapper);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, list);
    }

    /**
     * 删除收藏
     *
     * @param collectionsRequestParam
     * @param httpServletRequest
     * @return
     */
    @RequestMapping(value = "deleteCollections", method = RequestMethod.POST)
    public BaseResponse deleteCollections(@RequestBody @Validated CollectionsRequestParam collectionsRequestParam, HttpServletRequest httpServletRequest) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userId", httpServletRequest.getAttribute("userId"));
        queryWrapper.eq("ArticleId", collectionsRequestParam.getArticleId());
        collectionsMapper.delete(queryWrapper);
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, null);
    }

    @RequestMapping(value = "selectMyCollections", method = RequestMethod.POST)
    public BaseResponse selectMyCollections(HttpServletRequest httpServletRequest) {
        List<CollectionsResponseParam> list = collectionsMapper.selectMyCollections((String) httpServletRequest.getAttribute("userId"));
        return BaseResponse.info(ResponseCodeEnum.SUCCESS, list);
    }
}
